package com.skt.controller;

import com.skt.domain.DTO.request.ChangePasswordRequest;
import com.skt.domain.DTO.request.UpdateProfileRequest;
import com.skt.domain.VO.UserVO;
import com.skt.exception.BusinessException;
import com.skt.service.ProfileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Tag(name = "用户信息接口")
public class ProfileController {

    private final ProfileService profileService;

    @Operation(
            summary = "获取当前用户信息",
            description = "获取当前登录用户的基本信息。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功，返回用户基本信息"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "404", description = "用户信息不存在")
    })
    @GetMapping("/me")
    public ResponseEntity<UserVO> getCurrentUserInfo() {

        log.info("获取当前用户信息请求");
        try {
            UserVO userVO = profileService.getCurrentUserInfo();
            log.info("获取当前用户信息成功: userId={}", userVO.getId());
            return ResponseEntity.ok(userVO);
        } catch (BusinessException e) {
            log.warn("获取当前用户信息失败: error={}", e.getMessage());
            throw e;
        }
    }


    @Operation(
            summary = "更新个人资料",
            description = "更新当前登录用户的个人信息。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新成功，返回更新后的用户信息"),
            @ApiResponse(responseCode = "400", description = "请求参数无效或业务规则校验失败"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "404", description = "用户信息不存在"),
            @ApiResponse(responseCode = "409", description = "用户名或邮箱已存在")
    })
    @PutMapping("/profile")
    public ResponseEntity<UserVO> updateUserProfile(@Valid @RequestBody UpdateProfileRequest updateRequest) {
        log.info("更新用户资料请求: {}", updateRequest);
        try {
            UserVO userVO = profileService.updateUserProfile(updateRequest);
            log.info("更新用户资料成功: userId={}", userVO.getId());
            return ResponseEntity.ok(userVO);
        } catch (BusinessException e) {
            log.warn("更新用户资料失败: error={}", e.getMessage());
            throw e;
        }
    }


    @Operation(
            summary = "修改密码",
            description = "修改当前登录用户的密码，需要验证旧密码。修改成功后需要重新登录。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "密码修改成功"),
            @ApiResponse(responseCode = "400", description = "请求参数无效、密码强度不足或业务规则校验失败"),
            @ApiResponse(responseCode = "401", description = "未授权访问或旧密码验证失败"),
            @ApiResponse(responseCode = "403", description = "操作频率过高或账户被临时锁定"),
            @ApiResponse(responseCode = "404", description = "用户信息不存在"),
            @ApiResponse(responseCode = "429", description = "请求过于频繁，请稍后重试")
    })
    @PostMapping("/change-password")
    public ResponseEntity<Void> changePassword(@Valid @RequestBody ChangePasswordRequest changePasswordRequest) {
        log.info("修改密码请求");
        try {
            profileService.changePassword(changePasswordRequest);
            log.info("修改密码成功");
            return ResponseEntity.ok().build();
        } catch (BusinessException e) {
            log.warn("修改密码失败: error={}", e.getMessage());
            throw e;
        }
    }
}